
let middlewares=[
     (ctx,next)=>{
        console.log('Start:',1);
         next();
        console.log('End:',1);
    },
     (ctx,next)=>{
        console.log('Start:',2);
         next();
        console.log('End:',2);
    },
     (ctx,next)=>{
        console.log('Start:',3);
         next();
        console.log('End:',3);
    }
];


(()=>{
    let fn=compose(middlewares);
    console.log(fn);
    //fn('ctx');
})();

function compose(middlewares) {

    (function next(i){
        let fn=middlewares[i];

        if(!fn)return;
        return fn('ctx',()=>{
            return next(++i);
        });
    })(0);
}